package com.mz.ozy.activity.assets;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.adapter.BalanceDetailAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.BalanceDetailBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.ExchangePresenter;
import com.mz.ozy.mvp.view.ExchangeContract;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.widget.dialog.PayPopupWindow;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BalanceExchangeActivity extends BaseActivity<ExchangeContract.View, ExchangePresenter> implements ExchangeContract.View {

    @BindView(R.id.tv_wallet_num)
    TextView tvWalletNum;
    @BindView(R.id.tv_wallet_unit)
    TextView tvWalletUnit;
    @BindView(R.id.tv_exchange_num)
    TextView tvExchangeNum;
    @BindView(R.id.tv_exchange_label)
    TextView tvExchangeLabel;
    @BindView(R.id.tv_bonus_label)
    TextView tvBonusLabel;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.et_amount)
    EditText etAmount;

    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.rl_smartRefreshLayout)
    RelativeLayout mRlsmartRefreshLayout;
    private BalanceDetailAdapter mHomeAdapter;

    private int pageNo = 1;
    private int tag = 0;

    private TextWatcher mTextWatcherBAA = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            Utils.editTextUtils(editable, etAmount);
            if (TextUtils.isEmpty(editable.toString()) || editable.toString().equals(".") || Double.parseDouble(editable.toString()) == 0) {
//                etEthNumber.setText("0.00");
                tvExchangeNum.setText("=0.00" + getString(R.string.gold));
            } else {
               // tvExchangeNum.setText("=" + Utils.subFourNumber(Double.parseDouble(editable.toString()) * AssetsActivity.yxprice) + getString(R.string.gold));
            }
        }
    };
    private List<BalanceDetailBean.PageBean.PageItemsBean> mBeanList = new ArrayList<>();

    private int currentType = 0;
    private String amt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_exchange);
        ButterKnife.bind(this);
        initTitle();
        amt = getIntent().getStringExtra("amt");
        tvTitle.setVisibility(View.GONE);
        tvCenterTitle.setText(getString(R.string.assets_wallet));
        tvWalletUnit.setText("ANG");
        tvWalletNum.setText(amt);
        tvExchangeNum.setText("=0.00" + getString(R.string.gold));
        initView();

        mPresenter.getExchangeInfo(this);
        tvBonusLabel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentType == 5) {
                    currentType = 0;
                    tvExchangeLabel.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
                    tvExchangeLabel.setTextColor(ContextCompat.getColor(BalanceExchangeActivity.this, R.color.c_707477));
                    tvBonusLabel.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 17);
                    tvBonusLabel.setTextColor(ContextCompat.getColor(BalanceExchangeActivity.this, R.color.c_4298fd));

                    tag = 0;
                    pageNo = 1;
                    mPresenter.getExchangeInfo(BalanceExchangeActivity.this);

                }
            }
        });
        tvExchangeLabel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentType == 0) {
                    currentType = 5;
                    tvExchangeLabel.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 17);
                    tvExchangeLabel.setTextColor(ContextCompat.getColor(BalanceExchangeActivity.this, R.color.c_4298fd));
                    tvBonusLabel.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
                    tvBonusLabel.setTextColor(ContextCompat.getColor(BalanceExchangeActivity.this, R.color.c_707477));

                    tag = 0;
                    pageNo = 1;
                    mPresenter.getExchangeInfo(BalanceExchangeActivity.this);

                }
            }
        });
    }

    private void initView() {
        etAmount.addTextChangedListener(mTextWatcherBAA);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mHomeAdapter = new BalanceDetailAdapter(this, mBeanList);
        recyclerview.setAdapter(mHomeAdapter);
        recyclerview.setNestedScrollingEnabled(false);
        llNoData.setVisibility(View.GONE);
        smartRefreshLayout.setEnableAutoLoadmore(true);
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.getExchangeInfo(BalanceExchangeActivity.this);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 0;
                pageNo = 1;
                mPresenter.getExchangeInfo(BalanceExchangeActivity.this);
            }
        });
    }

    @Override
    public void getExchangeInfoSuccess(BalanceDetailBean bean) {
        if (tag == 0) {
            smartRefreshLayout.setLoadmoreFinished(false);
            smartRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.getPage().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }

        } else if (tag == 1) {
            smartRefreshLayout.finishLoadmore();
        }
        mBeanList.addAll(bean.getPage().getPageItems());
        mHomeAdapter.notifyDataSetChanged();
        if (bean.getPage().getPageItems().size() == 0 || bean.getPage().getPageSize() * bean.getPage().getPageNumber() >= bean.getPage().getPageTotle()) {
            smartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }

    @Override
    public void onExchangeSuccess(Object o) {
        ToastUtils.showToast(getString(R.string.str_exchange_success));
        EventBus.getDefault().postSticky(Constants.EventCode.REFRESH_DETAIL_LIST);
        finish();
    }

    @Override
    public void onKtSuccess(BalanceDetailBean o) {

    }

    @Override
    public void onComitKtSuccess(Object o) {

    }

    @Override
    public void onXgKtSuccess(BalanceDetailBean o) {

    }

    @Override
    public void onXgComitSuccess(Object o) {

    }


    @OnClick({R.id.bt_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_confirm:
                final String amount = etAmount.getText().toString().trim();
                if (TextUtils.isEmpty(amount)) {
                    ToastUtils.showToast(getString(R.string.str_exchange_num_hint));
                    return;
                }
                showPaypwdPop();
                break;
        }
    }

    private PayPopupWindow mPayPopupWindow;

    public void showPaypwdPop() {
        if (mPayPopupWindow == null) {
            mPayPopupWindow = new PayPopupWindow(this, new PayPopupWindow.OnClickListener() {
                @Override
                public void onConfirm(String str) {
                    final String amount = etAmount.getText().toString().trim();
                    mPresenter.onExchange(BalanceExchangeActivity.this, amount, str);
                    mPayPopupWindow.dismiss();
                }
            });
        }
        mPayPopupWindow.showAtLocation(titleBar, Gravity.BOTTOM, 0, 0);


    }
}
